import MyModal from "@/components/MyModal";
import { EmailReg, IDCardReg, PhoneReg } from "@/constants/reg";
import {
  addSystemStaff,
  editSystemStaff,
  getSystemStaffDetail,
} from "@/services/systemStaff/systemStaff";

import { useBoolean, useRequest } from "ahooks";
import { Form, Input, message } from "antd";

import { useMemo, useRef } from "react";

export default ({ open, close, refresh, id }) => {
  const [form] = Form.useForm();
  const [loading, { toggle: toggleLoading }] = useBoolean(false);
  const fixedParams = useRef(null)//固定参数
  const onOk = async () => {
    const values = await form.validateFields();
    const fun = id ? editSystemStaff : addSystemStaff;
    toggleLoading();
    try {
      await fun({ ...values,...fixedParams?.current, sysStaffId: id,role:2 });
      message.success(id ? "修改成功" : "新增成功");
      close();
      refresh();
    } finally {
      toggleLoading();
    }
  };
  const { loading: queryLoading } = useRequest(getSystemStaffDetail, {
    manual: !id,
    defaultParams: [id],
    onSuccess: (res) => {
      form.setFieldsValue(res);
      fixedParams.current ={
        userBasicInfoId:res?.userBasicInfoId
      } 
    },
  });

  const title = useMemo(() => {
    return (id ? "修改" : "新增") + "系统工作人员";
  }, [id]);
  return (
    <MyModal
      title={title}
      open={open}
      close={close}
      onOk={onOk}
      queryLoading={queryLoading}
      confirmLoading={loading}
    >
      <Form form={form} wrapperCol={{ span: 24 }} layout="vertical">
        <Form.Item
          name="realName"
          rules={[
            {
              required: true,
            },
          ]}
          label="姓名"
        >
          <Input />
        </Form.Item>
        <Form.Item
          name="identifyNum"
          label="身份证号码"
          rules={[
            {
              required: true,
            },
            {
              pattern: IDCardReg,
              message: "请输入正确的身份证号码",
            },
          ]}
        >
          <Input />
        </Form.Item>
        <Form.Item
          name="tel"
          label="手机号码"
          rules={[
            {
              required: true,
            },
            {
              pattern: PhoneReg,
              message: "请输入正确的手机号码",
            },
          ]}
        >
          <Input />
        </Form.Item>
      </Form>
    </MyModal>
  );
};
